package cn.iocoder.yudao.module.biz.controller.admin.expand.view;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.excel.core.util.poi.ExcelUtilRuoyi;
import cn.iocoder.yudao.module.biz.controller.admin.contract.vo.ContractArchivePageReqVO;
import cn.iocoder.yudao.module.biz.controller.admin.contract.vo.ContractArchiveRespVO;
import cn.iocoder.yudao.module.biz.controller.admin.expand.view.domain.SanBizExpandRecordView;
import cn.iocoder.yudao.module.biz.controller.admin.expand.view.service.SanBizExpandRecordViewService;
import cn.iocoder.yudao.module.biz.controller.admin.receivables.domain.SanBizContractArchiveReceiveRecord;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 获取拓展监控大屏统计")
@RestController
@RequestMapping("/san/biz-expand-target-view")
@Validated
public class ExpandTargetViewController {

    @Resource
    private SanBizExpandRecordViewService expandTargetService;

    @PostMapping("/page")
    @Operation(summary = "获取拓展记录统计分页")
    @PreAuthorize("@ss.hasPermission('invoice:biz-invoice-management:query')")
    public CommonResult<IPage<SanBizExpandRecordView>> getPage(@Valid SanBizExpandRecordView pageReqVO) {
        IPage<SanBizExpandRecordView> pageResult = expandTargetService.getPage(pageReqVO);
        return success(pageResult);
    }

    @PostMapping("/export-excel")
    @Operation(summary = "导出拓展记录统计 Excel")
    @PreAuthorize("@ss.hasPermission('invoice:biz-invoice-management:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBizInvoiceManagementExcel(@Valid SanBizExpandRecordView pageReqVO,
                                                HttpServletResponse response) throws IOException {
//        QueryWrapper w = new QueryWrapper(pageReqVO);
        List<SanBizExpandRecordView> list = expandTargetService.getPage(pageReqVO).getRecords();
        ExcelUtilRuoyi<SanBizExpandRecordView> util = new ExcelUtilRuoyi<SanBizExpandRecordView>(SanBizExpandRecordView.class);
        SimpleDateFormat sft = new SimpleDateFormat("yyyy年MM月dd日");
        String d1 = "",d2="";
        if(pageReqVO.getExpandTimeStr() != null && pageReqVO.getExpandTimeStr().size() > 1){
             d1 = sft.format(pageReqVO.getExpandTimeStr().get(0));
             d2 = sft.format(pageReqVO.getExpandTimeStr().get(1));
        }
        util.exportExcel(response, list, d1 + "至"+d2+"历史拓展客户清单");
    }

    @PostMapping("/getCountSum")
    @Operation(summary = "获取本月拓展，本月拜访，签约户数，签约金额，上月签约户数，本月到期收款，逾期未续签，到期续签")
    @Parameter(name = "month", description = "查询月份", required = true)
    @PreAuthorize("@ss.hasPermission('invoice:biz-invoice-management:query')")
    public CommonResult<Map<String,Object>> getCountSum(String month) {
        Map<String,Object> pageResult = expandTargetService.getCountSum(month);
        return success(pageResult);
    }

    @PostMapping("/getContractArchiveViewList")
    @Operation(summary = "本月新签，到期续签，待续签合同列表 is_renew 1 已续签，0 新签；status '3' 待续签")
    @PreAuthorize("@ss.hasPermission('invoice:biz-invoice-management:query')")
    public CommonResult<IPage<ContractArchiveRespVO>> getContractArchiveViewList(@Valid ContractArchivePageReqVO pageReqVO) {
        IPage<ContractArchiveRespVO> pageResult = expandTargetService.getContractArchiveViewList(pageReqVO);
        return success(pageResult);
    }

    @PostMapping("/getContractReceiveRecordViewList")
    @Operation(summary = "本月到期收款列表 status = '1' 已收款")
    @PreAuthorize("@ss.hasPermission('invoice:biz-invoice-management:query')")
    public CommonResult<IPage<SanBizContractArchiveReceiveRecord>> getContractReceiveRecordViewList(@Valid SanBizContractArchiveReceiveRecord pageReqVO) {
        IPage<SanBizContractArchiveReceiveRecord> pageResult = expandTargetService.getContractReceiveRecordViewList(pageReqVO);
        return success(pageResult);
    }

    @PostMapping("/getTotalAmount")
    @Operation(summary = "获取一年拓展用户数拓展次数月份统计")
//    @Parameter(name = "month", description = "查询月份", required = true)
    @PreAuthorize("@ss.hasPermission('invoice:biz-invoice-management:query')")
    public CommonResult<List<Map<String,Object>>> getTotalAmount(String year) {
        List<Map<String,Object>> pageResult = expandTargetService.getTotalAmount(year);
        return success(pageResult);
    }
}
